﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.AuditLogging.StorageService.Management;
using PSE.Deployment.ConfigurationManager.Controls;
using System.Diagnostics;

namespace PSEComponents.Deployment.ConfigurationManager.Controls.AuditLoggingServiceControl
{
    public class StorageServiceControl : ExtendedServiceControl
    {
        public StorageServiceControl(PSE.Deployment.SetupBase.Configuration.WindowsService service)
            : base(service)
        {
        }

        protected override List<string> GetConfiguredProducts()
        {
            try
            {
                PSE.AuditLogging.StorageService.Management.ConfigurationManager manager = new PSE.AuditLogging.StorageService.Management.ConfigurationManager();
                return manager.GetConfiguredProducts();
            }
            catch (Exception ex)
            {
                //logar o erro
                EventLog.WriteEntry("PSEComponents - Configuration Manager", ex.ToString());

                //retornar lista vazia pois assim o controle ira funcionar como um controle de servico normal permitindo iniciar e parar o servico
                //mas nao as instancias executando abaixo do servico
                return new List<string>();
            }
        }

        protected override List<string> GetRunningProducts()
        {
            try
            {
                PSE.AuditLogging.StorageService.Management.ServiceManager manager = new PSE.AuditLogging.StorageService.Management.ServiceManager();
                if (manager.IsRunning())
                    return manager.GetRunningProducts();
                else
                    return new List<string>();
            }
            catch (Exception ex)
            {
                //logar o erro
                EventLog.WriteEntry("PSEComponents - Configuration Manager", ex.ToString());

                //retornar lista vazia pois assim o controle ira funcionar como um controle de servico normal permitindo iniciar e parar o servico
                //mas nao as instancias executando abaixo do servico
                return new List<string>();
            }
        }

        protected override bool StartProduct(string product)
        {
            try
            {
                PSE.AuditLogging.StorageService.Management.ServiceManager manager = new PSE.AuditLogging.StorageService.Management.ServiceManager();
                if (manager.IsRunning())
                {
                    return manager.StartProduct(product);
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                //logar o erro
                EventLog.WriteEntry("PSEComponents - Configuration Manager", ex.ToString());

                //retornar lista vazia pois assim o controle ira funcionar como um controle de servico normal permitindo iniciar e parar o servico
                //mas nao as instancias executando abaixo do servico
                return false;
            }
        }

        protected override bool StopProduct(string product)
        {
            try
            {
                PSE.AuditLogging.StorageService.Management.ServiceManager manager = new PSE.AuditLogging.StorageService.Management.ServiceManager();
                if (manager.IsRunning())
                {
                    return manager.StopProduct(product);
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                //logar o erro
                EventLog.WriteEntry("PSEComponents - Configuration Manager", ex.ToString());

                //retornar lista vazia pois assim o controle ira funcionar como um controle de servico normal permitindo iniciar e parar o servico
                //mas nao as instancias executando abaixo do servico
                return false;
            }
        }

    }
}
